from django.db import models
from django.contrib.auth.models import AbstractUser  # 继承了AbstractBaseUser，作为一个抽象类它包含完整的用户模型的字段

class UserProfile(AbstractUser):
    """ 用户信息 """
    name = models.CharField(max_length=20, default='', blank=True, verbose_name='真实姓名')
    introduction = models.CharField(max_length=20, default='', blank=True, verbose_name='职业')
    mobile = models.CharField(max_length=11, unique=True, null=True, blank=True, default=None, verbose_name='手机号码')
    image = models.ImageField(upload_to='avatar/%Y/%m', default='avatar/default.jpeg', blank=True, verbose_name='头像')

    class Meta:
        db_table = 'oauth_users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.username

    def _get_user_permissions(self):
        permissions = []
        if  "admin" in self.username:
            permissions.append('admin')
        return permissions

    def get_user_info(self):
        # 获取用户信息
        user_info = {
            'id': self.pk,
            'roles': self. _get_user_permissions(),
            'username': self.username,
            'introduction': self.introduction,
            'name': self.name,
            'avatar': '/media/' + str(self.image),
            'email': self.email,
            'mobile': '' if self.mobile is None else self.mobile
        }
        return user_info


